
cc.Class({
    extends: cc.Component,

    properties: {
        beginContactEvents:{
            default:[],
            type:[cc.Component.EventHandler],
        },
        endContactEvents:{
            default:[],
            type:[cc.Component.EventHandler],
        },
        preSolveEvents:{
            default:[],
            type:[cc.Component.EventHandler],
        },
        postSolveEvents:{
            default:[],
            type:[cc.Component.EventHandler],
        },
    },

    // 只在两个碰撞体开始接触时被调用一次
    onBeginContact: function (contact, selfCollider, otherCollider) {
        var eventName = 'onBeginContact';
        var events = this.beginContactEvents;
        this._onPhysicsEvent(eventName, events, contact, selfCollider, otherCollider);
    },

    // 只在两个碰撞体结束接触时被调用一次
    onEndContact: function (contact, selfCollider, otherCollider) {
        var eventName = 'onEndContact';
        var events = this.endContactEvents;
        this._onPhysicsEvent(eventName, events, contact, selfCollider, otherCollider);
    },

    // 每次将要处理碰撞体接触逻辑时被调用
    onPreSolve: function (contact, selfCollider, otherCollider) {
        var eventName = 'onPreSolve';
        var events = this.preSolveEvents;
        this._onPhysicsEvent(eventName, events, contact, selfCollider, otherCollider);
    },

    // 每次处理完碰撞体接触逻辑时被调用
    onPostSolve: function (contact, selfCollider, otherCollider) {
        var eventName = 'onPostSolve';
        var events = this.postSolveEvents;
        this._onPhysicsEvent(eventName, events, contact, selfCollider, otherCollider);
    },
    _onPhysicsEvent(eventName, events, contact, selfCollider, otherCollider){
        var e = new cc.Event.EventCustom(eventName);
        e.target = this.node;
        //
        e.detail = e.detail || {};
        e.detail.contact = contact;
        e.detail.selfCollider = selfCollider;
        e.detail.otherCollider = otherCollider;
        //
        cc.Component.EventHandler.emitEvents(events, e);
    },
    onPhysicsEventTest(e, customData){
    },

    // update (dt) {},
});
